home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / DefaultTvEventHandler.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  5.6 KB  |  232 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.awt.Button;
  4. import java.awt.Event;
  5. import java.awt.TextComponent;
  6. import java.awt.TextField;
  7.  
  8. public class DefaultTvEventHandler implements TvEventHandler {
  9.    Grid grid;
  10.    int rowSelectionBase = -1;
  11.  
  12.    public void setupView(Grid v) {
  13.       this.grid = v;
  14.    }
  15.  
  16.    public boolean handleCellEvent(Event e, TableCell cell) {
  17.       if (e.id != 59 && e.id != 56) {
  18.          this.grid.clearAllSelections();
  19.          return true;
  20.       } else {
  21.          return true;
  22.       }
  23.    }
  24.  
  25.    public boolean handleColHeadingEvent(Event e, TableCell cell) {
  26.       return true;
  27.    }
  28.  
  29.    public boolean handleRowHeadingEvent(Event e, TableCell cell) {
  30.       if (e.id == 51) {
  31.          boolean leftPressed = (e.modifiers & 8 & 4) == 0;
  32.          if (e.shiftDown() && leftPressed) {
  33.             if (this.grid.isSelectionBaseSet()) {
  34.                this.grid.selectRange(cell.row() + 1, 0);
  35.             } else {
  36.                this.grid.setSelectionBase(cell.row() + 1, 0);
  37.                this.grid.selectRange(cell.row() + 1, 0);
  38.             }
  39.          } else if (e.controlDown() && leftPressed) {
  40.             if (!this.grid.isRangeSelected()) {
  41.                this.grid.setSelectionBase(cell.row() + 1, 0);
  42.             }
  43.  
  44.             this.grid.toggleRow(cell.row() + 1);
  45.          } else if (leftPressed) {
  46.             boolean set = this.grid.isRowSet(cell.row() + 1);
  47.             this.grid.clearAllSelections();
  48.             this.grid.setSelectionBase(cell.row() + 1, 0);
  49.             this.grid.setRow(cell.row() + 1, !set);
  50.          }
  51.       }
  52.  
  53.       return true;
  54.    }
  55.  
  56.    public boolean handleCornerCellEvent(Event e, TableCell cell) {
  57.       return true;
  58.    }
  59.  
  60.    public boolean handleToolbarEvent(Event e) {
  61.       if (e.target instanceof TextField && e.id == 1001) {
  62.          TextField go = (TextField)e.target;
  63.  
  64.          try {
  65.             String rowText = ((TextComponent)go).getText();
  66.             if (rowText.equals("")) {
  67.                return true;
  68.             }
  69.  
  70.             int row = Integer.parseInt(rowText);
  71.             this.grid.gotoRow(row);
  72.          } catch (NumberFormatException ex) {
  73.             this.handleException((Coordinate)null, ex);
  74.          }
  75.       }
  76.  
  77.       if (e.target instanceof Button && (!(e.target instanceof Button) || e.id == 1001)) {
  78.          Button button = (Button)e.target;
  79.          String label = button.getLabel();
  80.          if (label.equals(Grid.gotoRowLabel)) {
  81.             try {
  82.                String rowText = (String)e.arg;
  83.                if (rowText.equals("")) {
  84.                   return true;
  85.                }
  86.  
  87.                int row = Integer.parseInt(rowText);
  88.                this.grid.gotoRow(row);
  89.             } catch (NumberFormatException ex) {
  90.                this.handleException((Coordinate)null, ex);
  91.             }
  92.          } else {
  93.             if (label.equals(Grid.appendRowLabel)) {
  94.                try {
  95.                   this.grid.appendRow();
  96.                } catch (TypeNotSupported ex) {
  97.                   this.handleException((Coordinate)null, ex);
  98.                }
  99.  
  100.                return true;
  101.             }
  102.  
  103.             if (label.equals(Grid.deleteRowLabel)) {
  104.                int[] rows = this.grid.getSelectedRows();
  105.                boolean auto = this.grid.getAutoRedraw();
  106.                this.grid.setAutoRedraw(false);
  107.  
  108.                try {
  109.                   if (rows.length == 0) {
  110.                      Coordinate c = this.grid.getCurrentCellCoordinates();
  111.                      if (c != null) {
  112.                         this.grid.deleteRow(c.row());
  113.                         this.grid.redraw(true);
  114.                      }
  115.                   } else {
  116.                      for(int i = rows.length - 1; i >= 0; --i) {
  117.                         this.grid.deleteRow(rows[i]);
  118.                      }
  119.                   }
  120.                } catch (TypeNotSupported ex) {
  121.                   this.handleException((Coordinate)null, ex);
  122.                }
  123.  
  124.                this.grid.setAutoRedraw(auto);
  125.                this.grid.clearAllSelections();
  126.                return true;
  127.             }
  128.  
  129.             if (label.equals(Grid.insertRowLabel)) {
  130.                int row = this.grid.getFirstSelectedRow();
  131.  
  132.                try {
  133.                   if (row != -1) {
  134.                      this.grid.insertRow(row);
  135.                   } else {
  136.                      Coordinate c = this.grid.getCurrentCellCoordinates();
  137.                      if (c != null) {
  138.                         this.grid.insertRow(c.row());
  139.                         this.grid.redraw(true);
  140.                      }
  141.                   }
  142.                } catch (TypeNotSupported ex) {
  143.                   this.handleException((Coordinate)null, ex);
  144.                }
  145.  
  146.                this.grid.clearAllSelections();
  147.                return true;
  148.             }
  149.  
  150.             if (label.equals(Grid.saveLabel)) {
  151.                try {
  152.                   this.grid.save();
  153.                } catch (TypeNotSupported ex) {
  154.                   this.handleException((Coordinate)null, ex);
  155.                }
  156.  
  157.                return true;
  158.             }
  159.  
  160.             if (label.equals(Grid.restartLabel)) {
  161.                this.grid.restart();
  162.                return true;
  163.             }
  164.  
  165.             if (label.equals(Grid.undoRowLabel)) {
  166.                int[] rows = this.grid.getSelectedRows();
  167.                boolean auto = this.grid.getAutoRedraw();
  168.                this.grid.setAutoRedraw(false);
  169.  
  170.                try {
  171.                   if (rows.length == 0) {
  172.                      Coordinate c = this.grid.getCurrentCellCoordinates();
  173.                      if (c != null) {
  174.                         this.grid.undoRow(c.row());
  175.                         this.grid.redraw(true);
  176.                      }
  177.                   } else {
  178.                      for(int i = rows.length - 1; i >= 0; --i) {
  179.                         this.grid.undoRow(rows[i]);
  180.                      }
  181.                   }
  182.                } catch (TypeNotSupported ex) {
  183.                   this.handleException((Coordinate)null, ex);
  184.                }
  185.  
  186.                this.grid.setAutoRedraw(auto);
  187.                this.grid.clearAllSelections();
  188.                return true;
  189.             }
  190.  
  191.             if (label.equals(Grid.undeleteRowLabel)) {
  192.                int[] rows = this.grid.getSelectedRows();
  193.                boolean auto = this.grid.getAutoRedraw();
  194.                this.grid.setAutoRedraw(false);
  195.  
  196.                try {
  197.                   if (rows.length == 0) {
  198.                      Coordinate c = this.grid.getCurrentCellCoordinates();
  199.                      if (c != null) {
  200.                         this.grid.undeleteRow(c.row());
  201.                         this.grid.redraw(true);
  202.                      }
  203.                   } else {
  204.                      for(int i = rows.length - 1; i >= 0; --i) {
  205.                         this.grid.undeleteRow(rows[i]);
  206.                      }
  207.                   }
  208.                } catch (TypeNotSupported ex) {
  209.                   this.handleException((Coordinate)null, ex);
  210.                }
  211.  
  212.                this.grid.setAutoRedraw(auto);
  213.                this.grid.clearAllSelections();
  214.                return true;
  215.             }
  216.          }
  217.  
  218.          return true;
  219.       } else {
  220.          return false;
  221.       }
  222.    }
  223.  
  224.    public void handleException(Coordinate pos, Exception exc) {
  225.       if (pos != null) {
  226.          System.out.println("GRID: Error occurred at row=" + pos.row() + " col=" + pos.col());
  227.       }
  228.  
  229.       ((Throwable)exc).printStackTrace();
  230.    }
  231. }
  232.